import time

def slow_function():
    time.sleep(2)
    return "慢函数执行完毕"

def fast_function():
    return "快速函数执行完毕"

# 手动添加计时功能(传统方式)
def slow_function_with_timer():
    start_time = time.time()
    result = slow_function()
    end_time = time.time()
    execution_time = end_time - start_time
    print(f"慢函数执行时间: {execution_time} 秒")
    return result

#=================== 装饰器 =========================
# 定义装饰器
def time_logger(func):
    def wrapper():
        start_time = time.time()
        result = func()             # 执行函数的功能
        end_time = time.time()
        print(f"{func.__name__}执行时间：{end_time - start_time:.2f}秒")
        return result
    return wrapper

# 使用装饰器
@time_logger
def slow_function():
    time.sleep(2)
    return "慢函数执行完毕"

@time_logger
def fast_function():
    return "快速函数执行完毕"

print(slow_function())
print(fast_function())
